using Domain.Entities;
using Domain.Entities.Enums;
using NHibernate;

namespace Domain.Services
{
    public static class UserForumPermissionService
    {
        /*****************************************************************************************
        * Getters
        * ***************************************************************************************/
        public static UserForumPermission GetByUserIdAndForumId(int userId, short forumId)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            return session.CreateQuery("from UserForumPermission ufp where ufp.User.Id = " + userId + " and ufp.Forum.Id = " + forumId).UniqueResult<UserForumPermission>();
        }

        /*****************************************************************************************
        * Adders
        * ***************************************************************************************/
        public static UserForumPermission Add(User user, Forum forum, Permission permissions)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                UserForumPermission userForumPermission = new UserForumPermission();
                userForumPermission.User = user;
                userForumPermission.Forum = forum;
                userForumPermission.Permissions = permissions;
                session.Save(userForumPermission);

                transaction.Commit();

                return userForumPermission;
            }
        }

        /*****************************************************************************************
        * Updaters
        * ***************************************************************************************/
        public static void Update(UserForumPermission userForumPermission, Permission permissions)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                userForumPermission.Permissions = permissions;
                session.SaveOrUpdate(userForumPermission);

                transaction.Commit();
            }
        }

        /*****************************************************************************************
        * Deleters
        * ***************************************************************************************/
        public static void Delete(UserForumPermission userForumPermission)
        {
            ISession session = NHibernateHelper.Instance.CurrentSession;
            using (ITransaction transaction = session.BeginTransaction())
            {
                session.Delete(userForumPermission);

                transaction.Commit();
            }
        }
    }
}
